package com.tech.platform.cms.admin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tech.platform.cms.admin.dao.mapper.TenantMapper;
import com.tech.platform.cms.admin.dao.mapper.UserMapper;
import com.tech.platform.cms.admin.dao.pojo.Tenant;
import com.tech.platform.cms.admin.dao.pojo.User;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import com.tech.platform.cms.common.model.PageResult;
import com.tech.platform.cms.common.utils.PasswordEncoder;
import com.tech.platform.cms.common.utils.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserService {

    private final static String DEFAULT_PASSWORD = "123@abc";

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TenantMapper tenantMapper;

    public CommonResult<String> add(User user){
        if(ValidationUtil.validateBean(user).hasErrors()){
            return new CommonResult<>(Constant.PARAM_ERROR,ValidationUtil.validateBean(user).getErrors());
        }

        //判断tenantId是否有效
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("identification",user.getTenantId()).eq("status",1);
        List<Tenant> tenants = tenantMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(tenants)){
            return new CommonResult<>(Constant.PARAM_ERROR,"租户标识不存在");
        }

        String salt = user.getUserName()+ LocalDate.now().toString();
        String password = PasswordEncoder.encode(DEFAULT_PASSWORD,salt);
        user.setLocked(0);
        user.setPassword(password);
        user.setSalt(salt);
        user.setSex(1);
        user.setCTime(new Date());
        userMapper.insert(user);
        return new CommonResult<>(Constant.SUCCESS,"success");
    }

    public PageResult<List<User>> page(int current,int size,int status,String keyword){
        if(StrUtil.isBlank(keyword)){
            keyword = "";
        }
        Page<User> page = new Page<>(current,size);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("locked",status).like("username",keyword);
        IPage<User> users = userMapper.selectPage(page,wrapper);
        return new PageResult<>(Constant.SUCCESS,"success",users.getTotal(),users.getRecords());
    }

    public CommonResult<String> update(User user){
        if(ValidationUtil.validateBean(user).hasErrors()){
            return new CommonResult<>(Constant.PARAM_ERROR,ValidationUtil.validateBean(user).getErrors());
        }

        //判断用户是否存在
        User userEntity = userMapper.selectById(user.getId());
        if(ObjectUtil.isEmpty(userEntity)){
            return new CommonResult<>(Constant.PARAM_ERROR,"用户不存在,修改失败");
        }
        userMapper.updateById(user);
        return new CommonResult<>(Constant.SUCCESS,"修改成功");
    }
}
